import { reqUserLogin ,reqUserLoginx } from '@/api/index'

const state = {
  //请求登陆传回来的数据
  userInfo: {
    name: localStorage.getItem('userName'),
    token:localStorage.getItem('usertoken')
  }
}
const actions = {
  //发送登陆请求，及登录页传过来的登陆数据
  async  userLogin({commit},obj) {
    const result = await reqUserLogin(obj)
    if (result.code === 200)
   {   commit('stockpile_register_data', result.data)
    //再储存一份到本地储存当中
    localStorage.setItem('userName', result.data.name)
      localStorage.setItem('usertoken', result.data.token)
    }
    else{
      console.log(result.message);
    }
  },

  async userLogout({ commit }) {
    const result = await reqUserLoginx()
    if (result.code === 200) {
      //先清本地储存的中name和token uuid要用所有不清除
      localStorage.removeItem('userName')
      localStorage.removeItem('token')
      commit('quit_empty')
    } else {
      console.log(result.message);
    }
  }
}

const mutations = {

  //储存登陆请求后返回的值
  stockpile_register_data(state,data) {
    state.userInfo=data
  },

  //退出清空值
  quit_empty(state) {
    state.userInfo.name=''
    state.userInfo.token=''
  }
}
const   getters= {}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
}